<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-card class="search-wrapper">
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="医生ID" prop="doctorId">
          <el-input v-model="queryParams.doctorId" placeholder="请输入医生ID" clearable style="width: 200px" />
        </el-form-item>
        <el-form-item label="处理状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择处理状态" clearable style="width: 200px">
            <el-option label="未处理" value="0" />
            <el-option label="已处理" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">搜索</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 表格区域 -->
    <el-card class="table-wrapper">
      <el-table v-loading="loading" :data="feedbackList">
        <el-table-column type="index" label="序号" width="70" />
        <el-table-column label="反馈ID" align="center" prop="feedbackId" />
        <el-table-column label="医生ID" align="center" prop="doctorId" />
        <el-table-column label="反馈内容" align="center" prop="content" :show-overflow-tooltip="true" />
        <el-table-column label="处理状态" align="center">
          <template #default="{ row }">
            <el-tag :type="row.status === '0' ? 'warning' : 'success'">
              {{ row.status === '0' ? '未处理' : '已处理' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="处理备注" align="center" prop="handleNote" :show-overflow-tooltip="true" />
        <el-table-column label="提交时间" align="center" prop="createTime" width="180" />
        <el-table-column label="操作" align="center" width="150">
          <template #default="{ row }">
            <el-button v-if="row.status == '0'" type="primary" link @click="handleFeedback(row)">处理</el-button>
            <el-button type="danger" link @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :total="total"
        :page-sizes="[10, 20, 30, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <!-- 处理反馈的对话框 -->
    <el-dialog v-model="dialogVisible" title="处理反馈" width="500px">
      <el-form :model="form" label-width="80px">
        <el-form-item label="处理备注" prop="handleNote">
          <el-input
            v-model="form.handleNote"
            type="textarea"
            placeholder="请输入处理备注"
            :rows="4"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitHandle">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import { listFeedback, handleFeedback, delFeedback } from '@/api/feedback'
import type { FeedbackData, FeedbackQuery, HandleFeedbackParams } from '@/api/feedback'
import { ElMessage } from 'element-plus'

// 查询参数
const queryParams = reactive<FeedbackQuery>({
  pageNum: 1,
  pageSize: 10,
  doctorId: undefined,
  status: undefined
})

const loading = ref(false)
const total = ref(0)
const feedbackList = ref<FeedbackData[]>([])
const dialogVisible = ref(false)
const form = reactive({
  feedbackId: undefined as number | undefined,
  handleNote: ''
})

// 获取反馈列表
const getList = async () => {
  loading.value = true
  try {
    const res = await listFeedback(queryParams)
    if (res.code == 200) {
      feedbackList.value = res.rows
      total.value = res.total
    }
  } catch (error) {
    console.error(error)
  }
  loading.value = false
}

// 查询
const handleQuery = () => {
  queryParams.pageNum = 1
  getList()
}

// 重置
const resetQuery = () => {
  queryParams.doctorId = undefined
  queryParams.status = undefined
  handleQuery()
}

// 处理反馈
const handleFeedback = (row: FeedbackData) => {
  form.feedbackId = row.feedbackId
  form.handleNote = ''
  dialogVisible.value = true
}

// 提交处理
const submitHandle = async () => {
  if (!form.handleNote) {
    ElMessage.warning('请输入处理备注')
    return
  }

  if (!form.feedbackId) {
    return
  }

  const params: HandleFeedbackParams = {
    feedbackId: form.feedbackId,
    handleNote: form.handleNote,
    status: '1'
  }

  try {
    const res = await handleFeedback(params)
    if (res.code == 200) {
      ElMessage.success('处理成功')
      dialogVisible.value = false
      getList()
    }
  } catch (error) {
    console.error('处理反馈失败:', error)
  }
}

// 删除反馈
const handleDelete = async (row: FeedbackData) => {
  try {
    await ElMessage.confirm('是否确认删除该反馈？')
    const res = await delFeedback(row.feedbackId)
    if (res.code == 200) {
      ElMessage.success('删除成功')
      getList()
    }
  } catch (error) {
    console.error('删除反馈失败:', error)
  }
}

// 分页
const handleSizeChange = (val: number) => {
  queryParams.pageSize = val
  getList()
}

const handleCurrentChange = (val: number) => {
  queryParams.pageNum = val
  getList()
}

onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
  
  .search-wrapper {
    margin-bottom: 20px;
  }
  
  .table-wrapper {
    .el-pagination {
      margin-top: 20px;
      justify-content: flex-end;
    }
  }
}
</style> 